<template>
  <div class="table-container">
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleAdd">新增</a-button>
        <!-- <a-button type="primary" :disabled="checkedKeys.length != 1" @click="toTemplateConfig">模板配置</a-button> -->
        <!-- <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls">导出</a-button> -->
        <!-- <a-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</a-button> -->
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <HubTeamTemplateModal @register="registerModal" @success="handleSuccess" />
    <!-- 表单区域 -->
    <HubTeamTemplateConfigModal @register="registerTemplateModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="emp-team-base-HubTeamTemplate" setup>
import { ref, nextTick, computed, unref, toRefs } from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { useModal } from '/@/components/Modal';
import { useListPage } from '/@/hooks/system/useListPage';
import HubTeamTemplateModal from './components/HubTeamTemplateModal.vue';
import HubTeamTemplateConfigModal from './components/HubTeamEvaluateIndexModal.vue';
import { columns, searchFormSchema } from './HubTeamTemplate.data';
import { pageList, deleteOne, batchDelete, getImportUrl, getExportUrl, } from './HubTeamTemplate.api';
const checkedKeys = ref<Array<string | number>>([]);
const checkedRows = ref<Array<string | number | object>>([]);
import { useMessage } from '/@/hooks/web/useMessage'; // 轻提醒
const { createMessage: $message } = useMessage(); // 解构

//注册model
const [registerModal, { openModal }] = useModal();
//注册模板model
const [registerTemplateModal, { openModal: openTemplateModal }] = useModal();
//注册table数据
const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
  tableProps: {
    title: 'hub_team_template',
    api: pageList,
    columns,
    canResize: false,
    // dataSource: mockDate,
    showIndexColumn: true,
    rowKey: 'id',
    beforeFetch: (params) => {
      return Object.assign(params);
    }, //表格和查询条件同步更新的方法
    formConfig: {
      //labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      fieldMapToNumber: [],
      fieldMapToTime: [],
    },
    actionColumn: {
      width: 180,
      fixed: 'right',
    },
    immediate: true,
  },
  exportConfig: {
    name: 'hub_team_template',
    url: getExportUrl,
  },
  importConfig: {
    url: getImportUrl,
    success: handleSuccess,
  },
});

const [registerTable, { getForm, reload, setTableData }, { rowSelection, selectedRowKeys, selectedRows }] = tableContext;
const onSelectChange = (selectedRowKeys, selectedRows) => {
  checkedKeys.value = selectedRowKeys;
  checkedRows.value = selectedRows;
};

const pageParams = {
  orgNameBase: '',
  orgCodeBase: '',
};

/**
 * 新增事件
 */
function handleAdd() {
  openModal(true, {
    isUpdate: false,
    showFooter: true,
    record: {
      orgName: '123',
      orgCode: getForm().getFieldsValue().orgCode,
    },
  });
}
/**
 * 编辑事件
 */
function handleEdit(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    showFooter: true,
  });
}
/**
 * 详情
 */
function handleDetail(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    showFooter: false,
  });
}
/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteOne({ id: record.id }, handleSuccess);
}
/**
 * 成功回调
 */
function handleSuccess() {
  (selectedRowKeys.value = []) && reload();
}
/**
 * 操作栏
 */
function getTableAction(record) {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
    },
    {
      label: '删除',
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      },
    },
    {
      label: '模块配置',
      onClick: toTemplateConfig.bind(null, record),
    },
  ];
}
// 配置模板
const toTemplateConfig = (record) => {
  // if (selectedRows.value.length === 0) {
  //   $message.error('请勾选一条模板');
  //   return;
  // }
  openTemplateModal(true, {
    templateCode: record.templateCode,
    orgCode: getForm().getFieldsValue().orgCode,
    templateParams: record,
  });
};
</script>

<style scoped lang="less">
/deep/.ant-table-wrapper {
  padding: 16px !important;
}
</style>
